package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AssignProductTagsDTO;
import com.nnnu.wsnackshop.pojo.dto.RemoveProductTagsDTO;
import com.nnnu.wsnackshop.pojo.vo.ProductTagsVO;
import com.nnnu.wsnackshop.pojo.vo.TagVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductTagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品-标签关联 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminProductTagsController")
@RequestMapping("/api/admin/product-tags")
@Tag(name = "管理端-商品标签关联接口")
@Slf4j
@Validated
@RequiredArgsConstructor
public class ProductTagsController {

    private final IProductTagsService productTagsService;

    @Operation(summary = "查询商品已关联标签")
    @GetMapping("/list/{productId}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("product:tag:read")
    public Result<ProductTagsVO> listByProduct(
            @PathVariable @Parameter(description = "商品 ID", in = ParameterIn.PATH) Long productId) {
        log.info("查询商品({})已关联标签", productId);
        List<TagVO> tags = productTagsService.listTagsByProduct(productId);
        ProductTagsVO vo = new ProductTagsVO();
        vo.setProductId(productId);
        vo.setTags(tags);
        return Result.success(vo, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "为商品批量分配标签", description = "仅 superAdmin 可访问")
    @PostMapping("/assign")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("product:tag:create")
    public Result<Void> assignTags(
            @Valid @RequestBody @Parameter(description = "商品标签分配 DTO") AssignProductTagsDTO dto) {
        log.info("为商品({})分配标签：{}", dto.getProductId(), dto.getTagIds());
        productTagsService.assignTags(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "批量移除商品标签关联", description = "仅 superAdmin 可访问")
    @DeleteMapping("/remove")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("product:tag:delete")
    public Result<Void> removeTags(
            @Valid @RequestBody @Parameter(description = "商品标签移除 DTO") RemoveProductTagsDTO dto) {
        log.info("为商品({})移除标签：{}", dto.getProductId(), dto.getTagIds());
        productTagsService.removeTags(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
